//  mixin
//这里引入需要使用公共组件和方法
//定义混入对象
import {
	mapState
} from 'vuex'
const Mixin = {
	data() {
		return {
			//将公共数据提取到这里  
			result: {},
			list: [],
			queryParams: {
				pageIndex:1,
				pageSize: 10,
			},
			num: 0,
			totalCount: 999,
			mixinApi: '',
			paramss: {},
		}
	},
	components: {}, //公共组件C
	methods: {
		async init(api,params){
			this.num = 0
			this.queryParams.pageIndex = this.num + 1
			return await this.getList(api,params)
		},
		async getList(api, params,) {
			this.queryParams.pageIndex = this.num + 1
			uni.showLoading({
				// title: '加载中...'
				title: this.$t('mixins.loading')
			})
			this.mixinApi = api || this.mixinApi
			this.paramss = Object.assign(this.queryParams, params)
			let {
				result
			} = await this.$http.get(this.mixinApi, this.paramss,false)
			uni.hideLoading()
			this.result = result || []
			if (this.paramss.pageIndex >1) {
				this.list.push(...this.result.items)
			} else {
				this.list = this.result.items
			}
			this.totalCount = this.result.total
		},


	}, //公共方法
	onReachBottom() {
		this.num++
		this.queryParams.pageIndex = this.num + 1
		if (!this.list) return
		if (this.list.length >= this.total) return uni.showToast({
			// title: '已经没有更多数据了',
			title: this.$t('mixins.noMoreData'),
			icon: 'none'
		})

		this.getList(this.api, this.paramss)
	},
	async onPullDownRefresh() {
		this.num = 0
		this.totalCount = 999
		await this.getList(this.api, this.paramss)
		setTimeout(function() {
			uni.stopPullDownRefresh();
		}, 1000);
	},
}
export default Mixin
